import jwt
from rest_framework.views import APIView
from rest_framework.response import Response
from .sers import *
from django.utils import timezone


class UserView(APIView):
    def post(self, request):
        name = request.data.get("name")
        pwd = request.data.get("pwd")
        user = Member.objects.filter(name=name)
        if user:
            return Response({"msg": "你已注册", "code": 400})
        else:
            user = Member.objects.create(name=name, pwd=pwd)

            if user:
                token = jwt.encode({"uid": user.id}, "123")
                return Response({"msg": "注册成功", "code": 200, "token": token, "counter": user.counter, "id": user.id})
            else:
                return Response({"msg": "注册失败", "code": 400})

    def patch(self, request):
        name = request.data.get("name")
        pwd = request.data.get("pwd")
        user = Member.objects.filter(name=name, pwd=pwd).first()
        if user:
            token = jwt.encode({"uid": user.id}, "123")
            return Response({"msg": "登录成功", "code": 200, "token": token, "counter": user.counter, "id": user.id})
        else:
            return Response({"msg": "登录失败", "code": 400})


class CoachView(APIView):
    def get(self, request):
        queryset = Coach.objects.all()
        ser = CoachSerializer(queryset, many=True)
        return Response(ser.data)


class AddCartView(APIView):
    def post(self, request):
        uid = request.data.get("uid")
        coach = request.data.get("coach")
        coach_obj = Coach.objects.get(id=coach)
        coach_obj.status_id = 2
        coach_obj.save()
        Cart.objects.create(member_id=uid, coach_id=coach)
        return Response({"msg": "选择成功", "code": 200})

    def put(self, request):
        uid = request.data.get("uid")
        coach = request.data.get("coach")
        cart_obj = Cart.objects.filter(member_id=uid, coach_id=coach, total=0).first()
        coach_obj = Coach.objects.get(id=coach)
        member_obj = Member.objects.get(id=uid)
        if member_obj:
            member_obj.counter += 1
            member_obj.save()
            if member_obj.counter >= 3:
                times = cart_obj.end_time - cart_obj.start_time
                total = (times.seconds / 3600) * cart_obj.coach.cost
                total2 = ('%.2f' % total)
                cart_obj.total = float(total2) * float(0.9)
                cart_obj.save()
            if cart_obj:
                end_time1 = timezone.now()
                cart_obj.end_time = end_time1
                cart_obj.save()
                times = cart_obj.end_time - cart_obj.start_time
                total = (times.seconds / 3600) * cart_obj.coach.cost
                total2 = ('%.2f' % total)
                cart_obj.total = total2
                cart_obj.save()
                coach_obj.status_id = 1
                coach_obj.save()
            return Response({"msg": "结束成功", "code": 200})
        else:
            print(coach)

            return Response({"msg": "结束失败", "code": 400})

    def get(self, request):
        uid = request.GET.get("uid")
        coach = request.GET.get("coach")
        if uid and coach:
            queryset = Cart.objects.filter(member_id=uid, coach_id=coach)
            ser = CartSerializer(queryset,many=True)
            return Response(ser.data)
        elif uid:
            uid = request.GET.get("uid")
            queryset = Cart.objects.filter(member_id=uid)
            ser = CartSerializer(queryset, many=True)
            return Response(ser.data)

    def patch(self, request):
        coach_id = request.GET.get("coach_id")
        queryset = Cart.objects.filter(coach_id=coach_id)
        ser = CartSerializer(queryset,many=True)
        return Response(ser.data)
